package org.cst.client;

import com.google.gwt.core.client.GWT;

import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.user.client.ui.TextBox;

public class CalculatorUI extends Composite {

	enum Operation{
		sum, substract, multiplication, division
	}
	
	private static CalculatorUIUiBinder uiBinder = GWT
			.create(CalculatorUIUiBinder.class);

	//Display
	@UiField TextBox txtDisplay;
	//Numbers buttons
	@UiField Button btnZero;
	@UiField Button btnOne;
	@UiField Button btnTwo;
	@UiField Button btnThree;
	@UiField Button btnFour;
	@UiField Button btnFive;
	@UiField Button btnSix;
	@UiField Button btnSeven;
	@UiField Button btnEight;
	@UiField Button btnNine;
	//Operators buttons	
	@UiField Button btnSum;	
	@UiField Button btnSubstract;
	@UiField Button btnMultiplication;
	@UiField Button btnDivision;
	//Other buttons
	@UiField Button btnResult;
	@UiField Button btnSeparator;
	@UiField Button button;
	
	//Varibles
	private double valueInMemory = 0;
	private boolean newValue = false;
	private boolean continueWritingValue = true;
	private Operation typeOfOperation;
		
	interface CalculatorUIUiBinder extends UiBinder<Widget, CalculatorUI> {
	}

	public CalculatorUI() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	//Click Events Numbers
	@UiHandler("btnOne")
	void onBtnOneClick(ClickEvent event) {
		if(newValue){
			this.clearScreen();
			newValue = false;
		}	
		this.txtDisplay.setText(this.txtDisplay.getText() +  btnOne.getText());
	}
	
	@UiHandler("btnTwo")
	void onBtnTwoClick(ClickEvent event) {
		if(newValue){
			this.clearScreen();
			newValue = false;
		}	
		this.txtDisplay.setText(this.txtDisplay.getText() +  btnTwo.getText());
	}
	
	@UiHandler("btnThree")
	void onBtnThreeClick(ClickEvent event) {
		if(newValue){
			this.clearScreen();
			newValue = false;
		}	
		this.txtDisplay.setText(this.txtDisplay.getText() +  btnThree.getText());
	}
	
	@UiHandler("btnFour")
	void onBtnFourClick(ClickEvent event) {
		if(newValue){
			this.clearScreen();
			newValue = false;
		}	
		this.txtDisplay.setText(this.txtDisplay.getText() +  btnFour.getText());
	}
	
	@UiHandler("btnFive")
	void onBtnFiveClick(ClickEvent event) {
		if(newValue){
			this.clearScreen();
			newValue = false;
		}	
		this.txtDisplay.setText(this.txtDisplay.getText() +  btnFive.getText());
	}
	
	@UiHandler("btnSix")
	void onBtnSixClick(ClickEvent event) {
		if(newValue){
			this.clearScreen();
			newValue = false;
		}	
		this.txtDisplay.setText(this.txtDisplay.getText() +  btnSix.getText());
	}
	
	@UiHandler("btnSeven")
	void onBtnSevenClick(ClickEvent event) {
		if(newValue){
			this.clearScreen();
			newValue = false;
		}	
		this.txtDisplay.setText(this.txtDisplay.getText() +  btnSeven.getText());
	}
	
	@UiHandler("btnEight")
	void onBtnEightClick(ClickEvent event) {
		if(newValue){
			this.clearScreen();
			newValue = false;
		}	
		this.txtDisplay.setText(this.txtDisplay.getText() +  btnEight.getText());
	}
	
	@UiHandler("btnNine")
	void onBtnNineClick(ClickEvent event) {
		if(newValue){
			this.clearScreen();
			newValue = false;
		}	
		this.txtDisplay.setText(this.txtDisplay.getText() +  btnNine.getText());
	}
	
	@UiHandler("btnZero")
	void onBtnZeroClick(ClickEvent event) {
		if(newValue){
			this.clearScreen();
			newValue = false;
		}	
		this.txtDisplay.setText(this.txtDisplay.getText() +  btnZero.getText());
	}
	
	//Click Events Operators
	
	@UiHandler("btnResult")
	void onBtnResultClick(ClickEvent event) {
		switch(typeOfOperation){
			case sum:
			    this.txtDisplay.setText(Double.toString(this.valueInMemory + Double.parseDouble(this.txtDisplay.getText())));
			    newValue = true;
				break;	
			case substract:
			    this.txtDisplay.setText(Double.toString(this.valueInMemory - Double.parseDouble(this.txtDisplay.getText())));
			    newValue = true;
				break;	
			case multiplication:
			    this.txtDisplay.setText(Double.toString(this.valueInMemory * Double.parseDouble(this.txtDisplay.getText())));
			    newValue = true;
				break;	
			case division:			    
				this.txtDisplay.setText(Double.toString(this.valueInMemory / Double.parseDouble(this.txtDisplay.getText())));
			    newValue = true;
				break;	
		}
	}
	
	@UiHandler("btnSum")
	void onBtnSumClick(ClickEvent event) {		
		typeOfOperation = Operation.sum;
		this.valueInMemory = Double.parseDouble(this.txtDisplay.getText());
		newValue = true;
	}
	
	@UiHandler("btnSubstract")
	void onBtnSubstractClick(ClickEvent event) {
		typeOfOperation = Operation.substract;
		this.valueInMemory = Double.parseDouble(this.txtDisplay.getText());
		newValue = true;
	}
	
	@UiHandler("btnMultiplication")
	void onBtnMultiplicationClick(ClickEvent event) {
		typeOfOperation = Operation.multiplication;
		this.valueInMemory = Double.parseDouble(this.txtDisplay.getText());
		newValue = true;
	}
	@UiHandler("btnDivision")
	void onBtnDivisionClick(ClickEvent event) {
		typeOfOperation = Operation.division;
		this.valueInMemory = Double.parseDouble(this.txtDisplay.getText());
		newValue = true;
	}
	
	//Other methods
	
	public void clearScreen(){
		this.txtDisplay.setText("");
	}
	
	
	
	@UiHandler("btnSeparator")
	void onBtnSeparatorClick(ClickEvent event) {
		if(newValue){
			this.clearScreen();
			newValue = false;
		}	
		this.txtDisplay.setText(this.txtDisplay.getText() +  btnSeparator.getText());
	}
	
	@UiHandler("button")
	void onButtonClick(ClickEvent event) {
		clearScreen();
	}
}
